public class MyQueue {
    static class ListNode {
        private int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    private ListNode front;//队头
    private ListNode rear;//队尾

    private int usedSize;

    public void offer(int val) {
        ListNode node = new ListNode(val);
        if (front == null) {
            front = node;
            rear = node;
        } else {
            //采用头插法
            front.prev = node;
            node.next = front;
            front = node;
        }
        usedSize++;
    }

    //出队列 相当于删除队尾结点
    public int poll() {
        //头插尾删
        if (rear == null) {
            return -1;
        } else if (front == rear) {
            //只有一个结点
            int ret = rear.val;
            front = null;
            rear = null;
            usedSize--;
            return ret;
        } else {
            int ret = rear.val;
            rear = rear.prev;
            rear.next = null;
            usedSize--;
            return ret;
        }
    }

    //获取队头
    public int peek() {
        if (rear == null) {
            return -1;
        }
        return front.val;
    }

    public int getUsedSize() {
        return usedSize;
    }

    public boolean isEmpty() {
        if (usedSize == 0) {
            return true;
        }
        return false;
    }
}
